(function($) {
    $.fn.lazyload = function(options) {
        /*合并参数*/
        var settings = {
            threshold: 0,//一般输入正数，提前开始加载
            failurelimit: 0, //最多显示的个数,一定要传
            event: "scroll",
            effect: "show",
            container: window
        };
        if (options) {
            $.extend(settings, options);
        }
        var elements = this;

        /*容器绑定滚动事件*/
        if ("scroll" == settings.event) {
            $(settings.container).bind("scroll", function(event) {
                var counter = 0;
                elements.each(function() {
                    if ($.abovethetop(this, settings) || $.leftofbegin(this, settings)) {} else {
                        /*下和右同时返回false时手动调用事件,也就是元素在容器内时*/
                        if (!$.belowthefold(this, settings) && !$.rightoffold(this, settings)) {
                            $(this).trigger("appear");
                        } else {
                            if (counter++ > settings.failurelimit) {
                                return false;
                            }
                        }
                    }
                });
                /*获取imgload loaded=false的 jq对象*/
                var temp = $.grep(elements, function(element) {
                    return !element.loaded //返回状态false的对象
                });
                elements = $(temp); //重新封装未渲染成功的图为jq对象
            })
        }

        /*遍历延迟加载的所有图片*/
        this.each(function() {
            var self = this;
            /*获取不到属性就把src绑定过来*/
            // if (undefined == $(self).attr("data-original")) {
            //     $(self).attr("data-original", $(self).attr("src"));
            // }
            /*默认好多情况满足一种loaded = false 情况1*/
            // if ("scroll" != settings.event || undefined == $(self).attr("src") || settings.placeholder == $(self).attr("src") || ($.abovethetop(self, settings) || $.leftofbegin(self, settings) || $.belowthefold(self, settings) || $.rightoffold(self, settings))) {
            //     if (settings.placeholder) {
            //         $(self).attr("src", settings.placeholder);
            //     } else {
            //         $(self).removeAttr("src");
            //     }
            //     self.loaded = false;
            // } else {
            // /*非正常情况下为loaded = true 情况2*/
            //     self.loaded = true;
            // }
            self.loaded = false;

            /*单个图片绑定自定义事件*/
            $(self).one("appear", function() {
                if (!this.loaded) { //情况1
                    // $("<img />").bind("load", function() {
                    //     $(self).hide().attr("src", $(self).attr("data-original"))[settings.effect](settings.effectspeed);
                    //     self.loaded = true;
                    // }).attr("src", $(self).attr("data-original"));
                    $(this).css('background-image', 'url('+$(self).attr('baseUrl')+')');
                    $(this).removeAttr('baseUrl');
                    setTimeout(function(){
                        $(self).find('.fetish-t , .fetish-tips').fadeIn(); //内容里的文字渐变出现
                    },600);
                    self.loaded = true;
                }
            });

            /*不是滚动事件时触发*/
            if ("scroll" != settings.event) {
                $(self).bind(settings.event, function(event) {
                    if (!self.loaded) {
                        $(self).trigger("appear");
                    }
                })
            }
        });
        /*初始化滚动一次*/
        $(settings.container).trigger(settings.event);
        /*返回所有图片jq对象*/
        return this
    };

    /*工具函数*/
    $.belowthefold = function(element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).height() + $(window).scrollTop();
        } else {
            var fold = $(settings.container).offset().top + $(settings.container).height()
        }
        return fold <= $(element).offset().top - settings.threshold;
    };
    $.rightoffold = function(element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).width() + $(window).scrollLeft();
        } else {
            var fold = $(settings.container).offset().left + $(settings.container).width()
        }
        return fold <= $(element).offset().left - settings.threshold;
    };
    $.abovethetop = function(element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).scrollTop();
        } else {
            var fold = $(settings.container).offset().top;
        }
        return fold >= $(element).offset().top + settings.threshold + $(element).height();
    };
    $.leftofbegin = function(element, settings) {
        if (settings.container === undefined || settings.container === window) {
            var fold = $(window).scrollLeft();
        } else {
            var fold = $(settings.container).offset().left;
        }
        return fold >= $(element).offset().left + settings.threshold + $(element).width();
    };
    $.extend($.expr[":"], {
        "below-the-fold": "$.belowthefold(a, {threshold : 0, container: window})",
        "above-the-fold": "!$.belowthefold(a, {threshold : 0, container: window})",
        "right-of-fold": "$.rightoffold(a, {threshold : 0, container: window})",
        "left-of-fold": "!$.rightoffold(a, {threshold : 0, container: window})"
    })
})(jQuery);